package com.ikingtech.platform.service.application.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.application.model.ApplicationModelFieldDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.application.entity.ApplicationModelFieldDO;
import com.ikingtech.platform.service.application.mapper.ApplicationModelFieldMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tie yan
 */
@Service
public class ApplicationModelFieldService extends ServiceImpl<ApplicationModelFieldMapper, ApplicationModelFieldDO> {

    public List<ApplicationModelFieldDTO> modelConvert(List<ApplicationModelFieldDO> entities) {
        return Tools.Coll.convertList(entities, fieldEntity -> {
            ApplicationModelFieldDTO field = Tools.Bean.copy(fieldEntity, ApplicationModelFieldDTO.class);
            if (null != field.getType()) {
                field.setTypeName(field.getType().description);
            }
            if (null != field.getDbTableFieldType()) {
                field.setTypeName(field.getDbTableFieldType().description);
            }
            if (null != field.getCompareType()) {
                field.setCompareTypeName(field.getCompareType().description);
            }
            return field;
        });
    }

    public void removeByModelId(String modelId) {
        this.remove(Wrappers.<ApplicationModelFieldDO>lambdaQuery().eq(ApplicationModelFieldDO::getModelId, modelId));
    }

    public List<ApplicationModelFieldDO> listByModelId(String modelId) {
        return this.list(Wrappers.<ApplicationModelFieldDO>lambdaQuery().eq(ApplicationModelFieldDO::getModelId, modelId).orderByAsc(ApplicationModelFieldDO::getSortOrder));
    }

    public List<ApplicationModelFieldDO> listByModelIds(List<String> modelIds) {
        return this.list(Wrappers.<ApplicationModelFieldDO>lambdaQuery().in(ApplicationModelFieldDO::getModelId, modelIds));
    }
}
